package com.xiaomage.ViewDraw16

import android.content.Context
import android.graphics.*
import android.view.View

/**
 * Created by Administrator on 2018/1/23.
 */
class MyView(context: Context):View(context) {
    private var paint= Paint();
    private  var bitmap=BitmapFactory.decodeResource(resources,R.drawable.dog)
    private var radius=1f
    private var dx=10f
    private var dy=10f
    private var showShadow=true
    init {
        //首先关闭硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE,null)
        paint.isAntiAlias=true
        paint.color= Color.GREEN
        paint.textSize=25f

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if(showShadow){
            paint.setShadowLayer(radius,dx,dy,Color.GRAY)
        }else{
           paint.clearShadowLayer()
        }

        canvas.drawText("小马哥",100f,100f,paint)

        canvas.drawCircle(200f,200f,50f,paint)

        canvas.drawBitmap(bitmap,null,
                Rect(100,300,500,700),paint)
    }

    public fun radiuAdd(){
        radius+=1
        postInvalidate()
    }
    public fun dxAdd5(){
        dx+=5
        postInvalidate()
    }
    public fun dyAdd5(){
        dy+=5
        postInvalidate()
    }
    public fun clearShadow1(){
        radius=0f
        postInvalidate()
    }
    public fun clearShadow2(){
        showShadow=false
        postInvalidate()
    }
}